import { LandUseTypeAnalysisParams, ReportParagraphBlock } from "./typings/index";
import {
    isEmpty, getCellValueTotalByColGroup, getPercent, presetWorkSheet,
    getTableMatrixRowTotalValueMax, generateWordParagraBlockByPlainText,
    removeAndAdjustColumn, getCellValueTotalByRowGroup,
    generateWordParagraBlockByTableGenerateResult, getCellValueTotalUniqueCountByColGroup
} from "./utils";


const socialEconomicStatistics = (params: LandUseTypeAnalysisParams): ReportParagraphBlock[] => {
    const {
        workSheet
    } = params;
    const newWorkSheet = presetWorkSheet(workSheet);
    const protectlandName = '保护地';
    let workSheet1 = [...newWorkSheet].slice(2, newWorkSheet.length);
    let tempParagraph1_1: string = `${protectlandName}周边地区现有`;
    let tempParagraph1_2: string = `${protectlandName}周边的地方经济以`;
    tempParagraph1_1 += `${getCellValueTotalUniqueCountByColGroup(workSheet1, 0, 0, workSheet1.length)}个县、`;
    tempParagraph1_1 += `${getCellValueTotalUniqueCountByColGroup(workSheet1, 1, 0, workSheet1.length)}个村。`;
    tempParagraph1_1 += `${protectlandName}内总人口${getCellValueTotalByColGroup(workSheet1, 2, 0, workSheet1.length)}人，`;
    tempParagraph1_1 += `共计${getCellValueTotalByColGroup(workSheet1, 3, 0, workSheet1.length)}户，`;
    //第二段
    const industrialValue1 = getCellValueTotalByColGroup(workSheet1, 5, 0, workSheet1.length - 1);
    const industrialValue2 = getCellValueTotalByColGroup(workSheet1, 6, 0, workSheet1.length - 1);
    const industrialValue3 = getCellValueTotalByColGroup(workSheet1, 7, 0, workSheet1.length - 1);
    // console.log("industrialValue1--->", industrialValue1);
    // console.log("industrialValue2--->", industrialValue2);
    // console.log("industrialValue3--->", industrialValue3);
    let arr = [industrialValue1, industrialValue2, industrialValue3];
    arr = arr.sort((a, b) => b - a);
    // console.log("排序后的arr--->", arr);
    switch (arr[0]) {
        case industrialValue1:
            tempParagraph1_2 += `${newWorkSheet[1][5]}为主，`;
            break;
        case industrialValue2:
            tempParagraph1_2 += `${newWorkSheet[1][6]}为主，`;
            break;
        case industrialValue3:
            tempParagraph1_2 += `${newWorkSheet[1][7]}为主，`;
            break;
        default:
            break;
    }
    tempParagraph1_2 += `${protectlandName}总产值${industrialValue1 + industrialValue2 + industrialValue3}万元，`;
    workSheet1.forEach((rowList, rowIndex) => {
        if (rowIndex == 0) {
            tempParagraph1_1 += '其中';
            tempParagraph1_2 += '其中';
        }
        tempParagraph1_1 += `${rowList[1]}现有居民${rowList[3]}户${rowList[2]}人`;
        tempParagraph1_2 += `${rowList[1]}总产值${getCellValueTotalByRowGroup(workSheet1, rowIndex, 5, 7)}万元，人均年收入${Number(rowList[4]).toFixed(1)}万元`;

        if (rowIndex < workSheet1.length - 1) {
            tempParagraph1_1 += '；';
            tempParagraph1_2 += '；';
        } else {
            tempParagraph1_1 += '。';
            tempParagraph1_2 += '。';
        }
    })
    let tempWordParahraphList1: ReportParagraphBlock[] = [];
    tempWordParahraphList1.push(generateWordParagraBlockByPlainText('社区经济统计', 'heading1'));
    tempWordParahraphList1.push(generateWordParagraBlockByPlainText(tempParagraph1_1, 'normalParagraph'))
    tempWordParahraphList1.push(generateWordParagraBlockByPlainText(tempParagraph1_2, 'normalParagraph'))
    tempWordParahraphList1.push(generateWordParagraBlockByPlainText(`表  ${protectlandName}社区情况统计表`, 'smallTableName'))
    tempWordParahraphList1.push(generateWordParagraBlockByTableGenerateResult({
        tableStyle: 'normalTable',
        tableData: workSheet,
        tableHeaderRowNumber: 1,
        tableRowSpanInfoList: [
            { rowIndex: 0, colIndex: 0, rowSpan: 2 },
            { rowIndex: 0, colIndex: 1, rowSpan: 2 },
            { rowIndex: 0, colIndex: 2, rowSpan: 2 },
            { rowIndex: 0, colIndex: 3, rowSpan: 2 },
            { rowIndex: 0, colIndex: 4, rowSpan: 2 },
        ],
        tableColSpanInfoList: [
            { rowIndex: 0, colIndex: 5, colSpan: 4 },
        ]
    }))
    return tempWordParahraphList1;
}


export default socialEconomicStatistics;